package cn.lz;

/**
 * @author: TT
 * @date: 2018/12/22
 * @time: 11:25
 * @version: V1.0
 */

import cn.lz.pojo.tang;
import com.alibaba.fastjson.JSONObject;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

/**
 * 客户端业务处理逻辑
 */
public class MinaClientHandler extends IoHandlerAdapter {

    /** 心跳包内容 */
    private static final String HEARTBEATREQUEST = "1111";
    private static final String HEARTBEATRESPONSE = "1112";

    // 当客户端连接进入时
    @Override
    public void sessionOpened(IoSession session) throws Exception {
        System.out.println("incomming 客户端: " + session.getRemoteAddress());
        tang t =new tang();
        t.setSixLen("000164");
        t.setCD("Z");
        t.setOD("5896348_1 ");
        t.setTY("TY");
        t.setID("3400001");
        t.setPW("895A4");
        t.setCC("13800138000");
        t.setTM("15922935317");
        t.setSL(5);
        t.setLO("116.341603,40.012486");
        t.setVR("1001");
        t.setSG("27,0");

        String jsonString = JSONObject.toJSONString(t);

        session.write( "000164"+jsonString);
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
        System.out.println("客户端发送信息异常....");
    }

    // 当客户端发送消息到达时
    @Override
    public void messageReceived(IoSession session, Object message)
            throws Exception {

        String s = message.toString();

        if (message.equals(HEARTBEATREQUEST)) {

            session.write(HEARTBEATRESPONSE);
        }
        System.out.println("服务器返回的数据：" + message.toString());
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        System.out.println("客户端与服务端断开连接.....");
    }

    @Override
    public void sessionCreated(IoSession session) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("one Client Connection" + session.getRemoteAddress());

//        session.write("我来了······");//发送消息
    }

}
